
#include "CStopWatch.h"

CStopWatch::CStopWatch(void)
{
	m_bIsRunning = false;
	m_dElapsedTime = 0.0;

	QueryPerformanceFrequency( (LARGE_INTEGER*)&m_llFrequency );

	QueryPerformanceCounter( (LARGE_INTEGER*)&m_llStartTick );
}

void CStopWatch::Start(void)
{
	if( !m_bIsRunning )
	{
		m_bIsRunning = true;

		QueryPerformanceCounter( (LARGE_INTEGER*)&m_llStartTick );
	}
}

void CStopWatch::Stop(void)
{
	if( m_bIsRunning )
	{
		m_bIsRunning = false;

		m_dElapsedTime = GetElapsedTime();
	}
}

void CStopWatch::Reset(void)
{
	m_dElapsedTime = 0.0;
	QueryPerformanceCounter( (LARGE_INTEGER*)&m_llStartTick );	
}

double CStopWatch::GetElapsedTime(void)
{
	double dt;

	if( m_bIsRunning )
	{
		LONGLONG	llStopTick;
		QueryPerformanceCounter( (LARGE_INTEGER*)&llStopTick );

		dt = (double)(llStopTick - m_llStartTick) / (double)m_llFrequency;
	}else{
		dt = m_dElapsedTime;
	}

	return dt;
}